package com.lonelyzhe.springboot.controller;

import com.lonelyzhe.springboot.bean.Category;
import com.lonelyzhe.springboot.service.impl.CategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("category")
@ResponseBody
public class CategoryController extends BaseController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/list")
    public AjaxResult list() {
        try {
            List<Category> categories = categoryService.selectCategoryList();
            return AjaxResult.success(categories);
        } catch (Exception e) {
//            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }

    @RequestMapping("/insert")
    public AjaxResult insertCategory(@RequestBody Category category) {
        try {
            categoryService.insertCategory(category);
            return AjaxResult.success();
        } catch (Exception e) {
//            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }
}
